<?php

namespace app\admin\controller\Article;

use app\admin\model\Article as ModelAlcitle;
use app\common\controller\AdminController;
use app\admin\traits\Curd;
use think\App;
use think\facade\Db;
use think\facade\Request;

use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;

class Article extends AdminController
{
    use Curd;

    protected $relationSearch = true; //开启关联查询

    public function __construct(App $app) //构造方法
    {
        parent::__construct($app);
        $this->model = new ModelAlcitle(); //当前模型== XyhCarouselImg 这张表
    }

    public function delete()
    {
        if (Request::isAjax()) {
            $id = Request::get('id');
            // dump($getdata);die;
            if (Db::name('article')->delete($id)) {
                return json(['msg' => '删除成功' , 'code' => '01']);
            }
            return json(['msg' => '删除失败' , 'code' => '05']);
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['cor_article'] = $post['cor_article'] . ',' . $post['cor_article_b'] . ',' . $post['cor_article'];
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['cor_article'] = $post['cor_article'] . ',' . $post['cor_article_b'] . ',' . $post['cor_article'];
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

}
